﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicPage;
using Xinkui.WebModel.Model;
using CommonFramework.Utility;
using Xinkui.WebModel.Helper;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel.Cache;
using Xinkui.Entity.AutoGenerator;
using Xinkui.WebModel;
using Xinkui.DataAccess.User;
using Xinkui.WebModel.Enums;

public partial class ContactSupplier : ForePage {


    protected char Action {
        get {
            return WebHelper.GetQueryStringValue<char>("act", 'c');
        }
    }


    protected int CompanyId {
        get {
            return WebHelper.GetQueryStringValue<int>("com", 0);
        }
    }





    protected void Page_Load(object sender, EventArgs e) {

        this.Enqueue(NavItem.Create("Contact Supplier"));

        List<ContactTarget> targets = new List<ContactTarget>(5);
        if (Action.Equals('c')) {
            var companyInfo = Singleton<CompanyInfoDataAccess>.Instance.CompanyInfo_View(CompanyId);
            if (companyInfo != null) {
                targets.Add(new ContactTarget {
                    Id = CompanyId,
                    Subject = companyInfo.CompanyName
                });
            }
        }
        else if (Action.Equals('p')) {
            int[] productIds = WebHelper.GetQueryStringValue<string>("ref").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(u => ConvertHelper.ToType<int>(u)).ToArray();
            for (int i = 0; i < productIds.Length; i++) {
                var pInfo = Singleton<ProductInfoDataAccess>.Instance.ProductInfo_View(productIds[i]);
                if (pInfo != null) {
                    var cInfo = Singleton<CompanyInfoDataAccess>.Instance.CompanyInfo_View(pInfo.CompanyID);
                    if (cInfo != null) {
                        targets.Add(new ContactTarget {
                            Id = pInfo.ProductID,
                            Subject = string.Format("{0}({1})", pInfo.ProductName, cInfo.CompanyName)
                        });
                    }
                }
            }
        }
        else {
            UIHelper.ShowErrorTips("Parameters Error");
            return;
        }

        rptDataSource.DataSource = targets;
        rptDataSource.DataBind();

        if (!IsPostBack) {
            rptCountrySource.DataSource = CountryCache.List().Values;
            rptCountrySource.DataBind();
        }
    }

    protected override void HandlePostActions() {
        string contactList = WebHelper.GetFormValue<string>("chkcontact", "");
        string subject = WebHelper.GetFormValue<string>("txtSubject");
        string message = WebHelper.GetFormValue<string>("txtMessage");
        string email = WebHelper.GetFormValue<string>("txtEmail");
        string name = WebHelper.GetFormValue<string>("txtName");
        int c = WebHelper.GetFormValue<int>("dropdownCountry", 0);
        int sourceId = ConvertHelper.ToType<int>(contactList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault());

        int userId = 0;
        if (this.HasLogin) {
            userId = this.LoginUser.UserID;
        }
        else {
            UnloginUserInfo u = new UnloginUserInfo();
            u.Source = (short)UnloginSourceEnum.Message;
            u.CreateDate = DateTime.Now;
            u.FirstName = name;
            u.Email = email;
            UserDataAccess.UnloginUserInfoAdd(u);
            userId = u.TempUserId;
        }
        string fileId = WebHelper.GetFormValue<string>("hidfilepath", "");
        MessageSendBox_Entity sendInfo = new MessageSendBox_Entity();
        sendInfo.IsDeleted = 0;
        sendInfo.MessageContent = message;
        sendInfo.MessageTitle = subject;
        sendInfo.MessageType = (short)MessageTypeEnum.InnerMessage;
        sendInfo.SendDate = DateTime.Now;
        sendInfo.SourceType = (short)(Action.Equals("p") ? MessageSourceTypeEnum.Product : MessageSourceTypeEnum.Company);
        sendInfo.SourceID = sourceId;
        sendInfo.UserID = userId;
        sendInfo.Attach = fileId;
        Xinkui.Common.Singleton<Xinkui.DataAccess.AutoGenerator.MessageSendBoxDataAccess>
             .Instance
             .MessageSendBox_Add(sendInfo);

        Xinkui.Entity.AutoGenerator.MessageInBox_Entity inbox = new Xinkui.Entity.AutoGenerator.MessageInBox_Entity();
        inbox.IsDeleted = 0;
        inbox.IsRead = 0;
        inbox.MessageID = sendInfo.MessageID;

        if (sendInfo.SourceType == (int)MessageSourceTypeEnum.Company) {
            var companyInfo = UserDataAccess.UserInfo_ViewByCompany(sourceId);
            if (companyInfo == null) {
                UIHelper.ShowErrorTips("Contact parameters has error");
                return;
            }
            inbox.UserID = companyInfo.UserID;
        }
        else {
            var product = Singleton<ProductInfoDataAccess>.Instance.ProductInfo_View(sourceId);
            if (product == null) {
                UIHelper.ShowErrorTips("Contact parameters has error");
                return;
            }
            inbox.UserID = product.UserID;
        }

        Xinkui.Common.Singleton<Xinkui.DataAccess.AutoGenerator.MessageInBoxDataAccess>
          .Instance
          .MessageInBox_Add(inbox);






        UIHelper.ShowSuucessTips("Contact infomation has sended");
    }
}


public class ContactTarget {
    public int Id { get; set; }

    public string Subject { get; set; }
}